<template>
	<el-container>
		<el-container>
			<div v-show="screenVisible" class="table-screen-column">
				<el-form label-width="auto" label-position="right">
					<el-row :gutter="20">
						<el-col :span="4">
							<el-form-item label="状态">
								<el-select v-model="search.status" clearable placeholder="状态搜索">
									<el-option v-for="item in statusLabel" :key="item.value" :label="item.label"
										:value="item.value">
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>
			<el-header>
				<div class="left-panel">
					<el-button v-auth="[':system:user:edit']" icon="el-icon-plus" type="primary" plain @click="add">新增</el-button>
					<el-button v-auth="[':system:user:resetpassword']" type="primary" plain :disabled="selection.length==0" @click="reset_password">密码重置</el-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keyword" placeholder="登录账号 / 手机号" clearable></el-input>
						<el-button type="primary" plain icon="el-icon-search" @click="upsearch">搜索</el-button>
						<el-button @click="resetQuery" icon="el-icon-refresh">重置</el-button>
						<el-button type="primary" link @click="screenVisible = !screenVisible">
							{{ screenVisible ? '关闭更多筛选' : '显示更多筛选' }}
							<el-icon><el-icon-arrow-down v-if="screenVisible" /><el-icon-arrow-up v-else /></el-icon>
						</el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" border remoteSort remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="ID" prop="id" width="80" sortable='custom'></el-table-column>
					<el-table-column label="头像" width="80" column-key="filterAvatar">
						<template #default="scope">
							<el-avatar :src="scope.row.avatar"></el-avatar>
						</template>
					</el-table-column>
					<el-table-column label="登录账号" prop="username" sortable='custom' column-key="filterUserName" :filters="[{text: '系统账号', value: '1'}, {text: '普通账号', value: '0'}]"></el-table-column>
					<el-table-column label="姓名" prop="nickname" sortable='custom'></el-table-column>
					<el-table-column label="手机号" prop="phone" sortable='custom'></el-table-column>
					<el-table-column label="所属分组" prop="group.name" sortable='custom'></el-table-column>
					<el-table-column label="所属角色" prop="role.name" sortable='custom'></el-table-column>
					<el-table-column label="状态" prop="status" width="150">
						<template #default="scope">
							<el-tag v-if="scope.row.status==1" type="success">启用</el-tag>
							<el-tag v-if="scope.row.status==0" type="danger">停用</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="创建时间" prop="created_at" width="170" sortable='custom'></el-table-column>
					<el-table-column label="操作" fixed="right" align="center" width="170">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
								<el-button v-auth="[':system:user:edit']" text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="primary" size="small" v-show="scope.row.id > 2">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
	<resetpassword-dialog v-if="dialog.resetpassword" ref="resetpasswordDialog" @success="handleSuccess" @closed="dialog.resetpassword=false">
	</resetpassword-dialog>

</template>

<script>
	import saveDialog from './save'
	import resetpasswordDialog from './resetpassword'

	export default {
		name: 'user',
		components: {
			saveDialog, resetpasswordDialog
		},
		data() {
			return {
				screenVisible: false,
				dialog: {
					save: false,
					resetpassword: false
				},
				apiObj: this.$API.system.user.list,
				selection: [],
				search: {
					keyword: '',
					status: '',
				},
				statusLabel: [
					{value: 0, label: '停用'},
					{value: 1, label: '正常'},
				]
			}
		},
		mounted() {
			
		},
		methods: {
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//重置密码
			reset_password() {
				this.dialog.resetpassword = true
				var userIds = []
				this.selection.forEach(item => {
					userIds.push(item.id)
				})
				if(userIds.length < 1) {
					this.$alert('请勾选重置的管理员', "提示", {type: 'error'})
					return
				}
				this.$nextTick(() => {
					this.$refs.resetpasswordDialog.open('edit').setData({ids: userIds})
				})
			},
			//删除
			async table_del(row, index){
				var reqData = {id: row.id}
				var res = await this.$API.demo.post.post(reqData);
				if(res.code == 200){
					//这里选择刷新整个表格 OR 插入/编辑现有表格数据
					this.$refs.table.tableData.splice(index, 1);
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
					})
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){console.log(selection)
				this.selection = selection;
			},
			//重置
			resetQuery() {
				this.search.status = ''
				this.search.keyword = ''
				this.upsearch()
			},
			//搜索
			upsearch(){
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSuccess(mode, search){
				if(mode != 'show') {
					this.search = search
					this.upsearch()
				}
			}
		}
	}
</script>
